package com.ohuang.demo

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.lifecycle.viewmodel.viewModelFactory
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.ohuang.demo.view.setKeypadAnimated
import com.ohuang.demo.viewModel.TestViewModel
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.launch


class ViewActivity : AppCompatActivity() {
    private val TAG = "ViewActivity"
    var lastEvent: MotionEvent? = null

    val recycleView: RecyclerView by lazy {
        findViewById(R.id.recycleView)
    }
    val editView: EditText by lazy {
        findViewById<EditText>(R.id.editView)
    }
    val main: ViewGroup by lazy {
        findViewById<ViewGroup>(R.id.main)
    }
    val tv_index: TextView by lazy {
        findViewById<TextView>(R.id.tv_index)
    }

    val child_Main: ViewGroup by lazy {
        findViewById<ViewGroup>(R.id.child_Main)
    }
    val testViewModel: TestViewModel by lazy {
        ViewModelProvider(this).get(TestViewModel::class.java)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        enableEdgeToEdge()

        setContentView(R.layout.activity_view)
//        ViewCompat.setOnApplyWindowInsetsListener(main) { v, insets ->
//            Log.d(TAG, "setOnApplyWindowInsetsListener: main")
//            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
//            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
//            insets
//
//        }


        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                testViewModel.testApiFlow.collect { value ->
                    Log.d(TAG, "${value?.区域} hashCode=${value.hashCode()}")
                }
            }
        }

        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                Log.d(TAG, " repeatOnLifecycle ${this@ViewActivity}")
                testViewModel.testFlow.collect { value ->
                    Log.d(TAG, "testFlow $value")
                }
            }
        }
//


        tv_index.setOnClickListener {
        }

        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)



        findViewById<View>(R.id.tv_button).setOnClickListener {
            startActivity(Intent(this, WebActivity::class.java))
        }
        recycleView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, true)

        recycleView.adapter = object : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
            override fun onCreateViewHolder(p0: ViewGroup, p1: Int): RecyclerView.ViewHolder {
                val textView = TextView(p0.context)
                return object : RecyclerView.ViewHolder(textView) {}
            }

            override fun getItemCount(): Int {
                return 50
            }

            override fun onBindViewHolder(p0: RecyclerView.ViewHolder, p1: Int) {
                val itemView = p0.itemView
                if (itemView is TextView) {
                    itemView.text = "item $p1"
                }
            }
        }

        setKeypadAnimated(listOf(recycleView, editView))
    }


}